﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SARD.Aux_Forms
{
    public partial class novolocal : Form
    {
        bool newplace;
        string sfile;
        string dirpath;

        /// <summary>
        /// Allows the user to see or change the machine properties.
        /// </summary>
        /// <param name="path">
        /// It's the path to the machine information text file.
        /// </param>
        /// <param name="newplace">
        /// It's used to create a new file or change an existent one.
        /// </param>
        public novolocal(string path, bool newplace)
        {
            InitializeComponent();

            dirpath = path;
            if (newplace) 
            {
                this.newplace = true;
            }
            
            else 
            {
                string[] file = Directory.GetFiles(path, "*.txt");
                sfile = file[0];
                string[] text = File.ReadAllLines(sfile);
                textBox1.Text = text[0].Substring("#Serviço:".Length + 1);
                textBox2.Text = text[1].Substring("#Endereço:".Length + 1);
                textBox3.Text = text[2].Substring("#Telefone:".Length + 1);
                textBox4.Text = text[3].Substring("#Responsável:".Length + 1);
                textBox5.Text = text[4].Substring("#Observações:".Length + 1);
            }


        }

        /// <summary>
        /// Allows the user to see or change the machine properties.
        /// </summary>
        public novolocal()
        {
            InitializeComponent();
            dirpath = Environment.CurrentDirectory;
            newplace = true;
        }

        /// <summary>
        /// This function closes the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        /// <summary>
        /// This function will save the desired data in the specific file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (newplace)
            {
                Directory.CreateDirectory(textBox1.Text.ToString());
                StreamWriter place = new StreamWriter(dirpath +"\\" + textBox1.Text.ToString() +"\\" + textBox1.Text.ToString() + ".txt");

                place.WriteLine("#Serviço: " + textBox1.Text.ToString());
                place.WriteLine("#Endereço: " + textBox2.Text.ToString());
                place.WriteLine("#Telefone: " + textBox3.Text.ToString());
                place.WriteLine("#Responsável: " + textBox4.Text.ToString());
                place.WriteLine("#Observações: " + textBox5.Text.ToString());
                place.WriteLine();
                place.Close();
            }
            else
            {
                string[] fulltext = File.ReadAllLines(sfile);



                fulltext[0] = "#Serviço: " + textBox1.Text.ToString();
                fulltext[1] = "#Endereço: " + textBox2.Text.ToString();
                fulltext[2] = "#Telefone: " + textBox3.Text.ToString();
                fulltext[3] = "#Responsável: " + textBox4.Text.ToString();
                fulltext[4] = "#Observações: " + textBox5.Text.ToString();
      

                File.WriteAllLines(sfile, fulltext);

                //Due to the Windows restrictions it is necessary to check the file name before saving.
                if (sfile != dirpath + "\\" + textBox1.Text.ToString() + ".txt")
                    File.Move(sfile, dirpath + "\\" + textBox1.Text.ToString() + ".txt");
                if (dirpath != Environment.CurrentDirectory + "\\" + textBox1.Text.ToString())
                    Directory.Move(dirpath , Environment.CurrentDirectory + "\\" + textBox1.Text.ToString());
 
            }

            this.Close();
            
        }


        /// <summary>
        /// The following functions will swap between the textbox and buttons of the graphic interface.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox2.Focus();
                e.Handled = true;
            }
        }



        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox3.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox1.Focus();
                e.Handled = true;
            }
        }



        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox4.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox2.Focus();
                e.Handled = true;
            }
        }



        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                textBox5.Focus();
                e.Handled = true;
            }

            if (e.KeyCode ==  Keys.Up)
            {
                textBox3.Focus();
                e.Handled = true;
            }
        }



        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                button2.Focus();
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Up)
            {
                textBox4.Focus();
                e.Handled = true;
            }
        }


    }
}
